﻿//using GoogleMobileAds.Api;
using UnityEngine;
using UnityEngine.Events;

public class RewardedVideoButton : MonoBehaviour
{
    private const string ACTION_NAME = "rewarded_video";
    public UnityEvent onRewarded;

    private void OnEnable()
    {
        Timer.Schedule(this, 0.1f, AddEvents);
    }

    private void AddEvents()
    {
#if UNITY_ANDROID || UNITY_IOS
        if (AdmobController.instance.rewardBasedVideo != null)
        {
            AdmobController.instance.rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        }
#endif
    }

    public void OnClick()
    {
        if (IsAdAvailable())
        {
#if UNITY_EDITOR
            onRewarded.Invoke();
#else
            //AdmobController.instance.ShowRewardBasedVideo();
#endif
        }
        else
        {
            Toast.instance.ShowMessage("Ad is not available now, please wait..");
        }

        Sound.instance.PlayButton();
    }

    //public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    //{
    //    onRewarded.Invoke();
    //}

    public bool IsAdAvailable()
    {
#if UNITY_ANDROID || UNITY_IOS
        if (AdmobController.instance.rewardBasedVideo == null) return false;
        bool isLoaded = AdmobController.instance.rewardBasedVideo.IsLoaded();
        return isLoaded;
#else
        return false;
#endif
    }

    private void OnDisable()
    {
#if UNITY_ANDROID || UNITY_IOS
        if (AdmobController.instance.rewardBasedVideo != null)
        {
            AdmobController.instance.rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
        }
#endif
    }
}
